<template>
  <div class="box-card">
    <div></div>
    <div>
      <el-input v-model="input" placeholder="根据用户名搜索"></el-input>
      <el-button>清空</el-button>
      <el-button type="primary">搜索</el-button>
      <el-button type="success" @click="opendialogfn">新增用户</el-button>
    </div>
    <div class="maincne">
      <el-alert type="info" :closable="false" class="incoTitle">
        <i class="el-icon-info" style="font-size: 16px"></i>
        数据一共{{ counts }}条
      </el-alert>
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="序号" width="180">
          </el-table-column>
          <el-table-column prop="email" label="邮箱" width="180">
          </el-table-column>
          <el-table-column prop="phone" label="联系电话" width="180">
          </el-table-column>
          <el-table-column prop="username" label="用户名" width="180">
          </el-table-column>
          <el-table-column
            prop="permission_group_title"
            label="权限组名称"
            width="180"
          >
          </el-table-column>
          <el-table-column prop="role" label="角色" width="180">
          </el-table-column>

          <el-table-column prop="name" label="操作" width="180">
            <template #default="{ row }">
              <el-button type="text" size="small" @click="bianjifn(row.id)">
                编辑
              </el-button>
              <el-button type="text" size="small" @click="deluserfn(row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="20"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
        >
        </el-pagination>
      </div>
      <!-- 碳层 -->
      <div>
        <el-dialog :title="title" :visible.sync="dialogFormVisible">
          <el-form :model="form" ref="addForm" :rules="addRules">
            <el-form-item
              label="用户名"
              :label-width="formLabelWidth"
              prop="username"
            >
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item
              label="邮箱"
              :label-width="formLabelWidth"
              prop="email"
            >
              <el-input v-model="form.email"></el-input>
            </el-form-item>
            <el-form-item
              label="密码"
              :label-width="formLabelWidth"
              prop="password"
            >
              <el-input v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item label="角色" :label-width="formLabelWidth">
              <el-input v-model="form.role"></el-input>
            </el-form-item>
            <el-form-item label="权限点名称" :label-width="formLabelWidth">
              <el-select
                v-model="form.permission_group_id"
                placeholder="请选择"
              >
                <el-option label="超级管理员组" :value="2"></el-option>
                <el-option label="编辑组" :value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="联系电话" :label-width="formLabelWidth">
              <el-input v-model="form.phone"></el-input>
            </el-form-item>
            <el-form-item label="介绍" :label-width="formLabelWidth">
              <el-input v-model="form.introduction"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="tijiaofn"> 确 定 </el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getuserListapi,
  adduserapi,
  edituserapi,
  editnewuserapi,
  deluserapi
} from '@/api/table'
export default {
  data() {
    return {
      input: '',
      newid: '',
      page: 1,
      pagesize: 50,
      counts: 2,
      tableData: [],
      // groupid: 0,
      dialogFormVisible: false,
      form: {
        avatar: '',
        username: '', // 用户名
        email: '', // 邮件
        // account: '', // 账号/密码
        introduction: '', // 介绍
        password: '',
        role: '', // 角色
        phone: '', // 电话
        permission_group_id: 0 // 权限点
        // fullName: '',
        // mobile: '',
        // avatar: '',
        // disabled: ''
      },
      formLabelWidth: '120px',
      addRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: ['blur', 'change']
          }
        ],
        email: [
          {
            required: true,
            message: '请输入邮箱',
            trigger: ['blur', 'change']
          }
        ],

        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  created() {
    this.getuserlist()
  },
  computed: {
    title() {
      return this.form.sex || this.form.sex === null ? '编辑用户' : '添加用户'
    }
  },
  methods: {
    // 请求用户列表
    async getuserlist() {
      const res = await getuserListapi(this.page, this.pagesize)
      this.tableData = res.data.list
      this.counts = +res.data.counts
      console.log(res)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    // 新增打开弹窗
    opendialogfn() {
      this.dialogFormVisible = true
    },
    async tijiaofn() {
      console.log(1)
      if (this.form.sex || this.form.sex === null) {
        const obj = {
          avatar: '',
          username: this.form.username, // 用户名
          email: this.form.email, // 邮件
          introduction: this.form.introduction, // 介绍
          password: this.form.password,
          role: this.form.role, // 角色
          phone: this.form.phone, // 电话
          permission_group_id: 0, // 权限点
          id: this.newid
        }
        console.log(this.form)
        await editnewuserapi(obj)
        this.$message.success('修改成功')
      } else {
        await adduserapi(this.form)
        this.$message.success('添加成功')
      }

      this.getuserlist()
      this.$refs.addForm.resetFields()
      this.form = {}
      this.dialogFormVisible = false
    },
    // 编辑
    async bianjifn(id) {
      this.dialogFormVisible = true
      console.log(id)
      const res = await edituserapi(id)
      this.form = res.data

      this.newid = res.data.id
      console.log(res)
    },
    async deluserfn(id) {
      await deluserapi(id)
      this.getuserlist()
    }
  }
}
</script>

<style scoped>
.box-card {
  padding-right: 15px;
  margin-top: 50px;
}
.el-input {
  width: 200px;
  margin: 10px;
}
.el-button--success {
  position: absolute;
  right: 10px;
  top: 10px;
}
.maincne {
  margin: 10px;
}
.block {
  margin: 10px;
  display: flex;
  justify-content: flex-end;
}
</style>
